package com.tencent.wxcloudrun.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tencent.wxcloudrun.domain.TStoreProdAttr;
import com.tencent.wxcloudrun.dto.StoreProdAttrReqDTO;
import com.tencent.wxcloudrun.mapper.TStoreProdAttrMapper;
import com.tencent.wxcloudrun.service.TStoreProdAttrService;

/**
 * @author ziyin.zhao
 * @description 针对表【t_store_prod_attr(商家商品属性表)】的数据库操作Service实现
 * @createDate 2025-04-22 16:43:56
 */
@Service
public class TStoreProdAttrServiceImpl extends ServiceImpl<TStoreProdAttrMapper, TStoreProdAttr>
    implements TStoreProdAttrService {

    @Override
    public void addProd(Long userId, Long prodId, List<StoreProdAttrReqDTO> prodAttrList) {
        if (CollectionUtils.isEmpty(prodAttrList)) {
            return;
        }
        List<TStoreProdAttr> storeProdAttrList = new ArrayList<>(prodAttrList.size());
        for (StoreProdAttrReqDTO reqDTO : prodAttrList) {
            TStoreProdAttr storeProdAttr = new TStoreProdAttr();
            storeProdAttr.setProdId(prodId);
            storeProdAttr.setName(reqDTO.getName());
            storeProdAttr.setValue(reqDTO.getValue());
            storeProdAttr.setStock(reqDTO.getStock());
            storeProdAttr.setCreatedTime(LocalDateTime.now());
            storeProdAttr.setUpdatedTime(LocalDateTime.now());
            storeProdAttr.setCreatedBy(String.valueOf(userId));
            storeProdAttr.setUpdatedBy(String.valueOf(userId));
            storeProdAttrList.add(storeProdAttr);
        }
        this.saveBatch(storeProdAttrList);
    }

    @Override
    public void deleteProd(Long prodId) {
        LambdaQueryWrapper<TStoreProdAttr> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TStoreProdAttr::getProdId, prodId);
        queryWrapper.eq(TStoreProdAttr::getEnableFlag, 1);
        this.remove(queryWrapper);
    }

    @Override
    public List<TStoreProdAttr> listProd(Long prodId) {
        LambdaQueryWrapper<TStoreProdAttr> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TStoreProdAttr::getProdId, prodId);
        queryWrapper.eq(TStoreProdAttr::getEnableFlag, 1);
        return this.list(queryWrapper);
    }

}
